﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows.Data;
using SoftSource.Common;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;
using SoftSource.Reactive;

namespace SoftSource.Timecard.ViewModels
{
    public class UtilizationSummaryViewModel : IUtilizationSummaryViewModel
    {
        public UtilizationSummaryViewModel()
        {
            this.Entries = new ObservableProperty<IEnumerable<EntrySummary>>();
        }

        #region IUtilizationSummaryViewModelConsume Members

        public IObservableProperty<IEnumerable<EntrySummary>> Entries { get; private set; }

        private PagedCollectionView users;
        public PagedCollectionView Users
        {
            get { return this.users; }
            set
            {
                this.users = value;
                this.Users.SortDescriptions.Add(new SortDescription(
                    ObjectReflection.GetPropertyName<UserSummary>(u => u.FullName),
                    ListSortDirection.Ascending));
                this.users.MoveCurrentToFirst();
                this.PropertyChanged.Notify(() => this.Users);
            }
        }

        public Func<DateTime, DateTime, IObservable<Unit>> LoadEntrySummaries { get; set; }

        #endregion

        #region IUtilizationSummaryViewModelService Members

        private IEnumerable<EntrySummary> entrySource;
        public IEnumerable<EntrySummary> EntrySource
        {
            get { return this.entrySource; }
            set
            {
                this.entrySource = value;
                this.Entries.Value = this.entrySource;
            }
        }

        private IEnumerable<UserSummary> userSource;
        public IEnumerable<UserSummary> UserSource
        {
            get { return this.userSource; }
            set
            {
                this.userSource = value;
                this.Users = new PagedCollectionView(this.userSource);
            }
        }

        #endregion

        #region IDataViewModelConsume Members

        public IObservable<Unit> Load { get; set; }

        #endregion

        #region INotifyPropertyChanged Members
        
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion

        ~UtilizationSummaryViewModel()
        {
            this.Dispose();
        }
    }
}
